<!--
* @description 修改巡检计划
* @author zywu
* @date 2021/03/04 09:49:48
!-->
<template>
  <el-dialog :close-on-click-modal="false" title="编辑巡检计划" width="500px" top="10vh" :visible.sync="visible" v-dialogDrag>
    <el-form label-width="100px" :model="dataForm" :rules="dataRules" ref="dataForm" style="padding-right:20px;">
      <el-form-item style="color:#999;">提示：计划创建后，只能修改计划结束日期。</el-form-item>
      <el-form-item label="计划名称：">{{dataForm.name}}</el-form-item>
      <el-form-item label="当前为：">
        <strong v-if="dataForm.endTimeStr">{{dataForm.endTimeStr}}结束</strong>
        <strong v-else>一直持续，没有结束日期</strong>
      </el-form-item>
      <el-form-item label="修改为：" prop="newEndDate">
        <el-date-picker
          v-model="dataForm.newEndDate"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择结束日期"
          :picker-options="pickerOptions"
          clearable
        ></el-date-picker>
        <p v-if="!dataForm.newEndDate">一直持续</p>
      </el-form-item>
      <el-form-item label-width="0" class="verifyidentity-button">
        <el-button round @click="visible=false">取消</el-button>
        <el-button type="primary" round @click="handleSave">确定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import util from '@/utils/util'
import { updateInspectPlan } from '@/api/inspectx'
import { mapState } from 'vuex'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        endDate: '',
      },
      dataRules: {},
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < new Date().setHours(0, 0, 0, 0) //不小于今天
        },
      },
    }
  },
  computed: {
    ...mapState(['userinfo']),
  },
  methods: {
    handleSave() {
      updateInspectPlan({
        id: this.dataForm.id,
        endDateStr: this.dataForm.newEndDate || '',
        account: this.userinfo.accountName,
      })
        .then((res) => {
          if (res.status == 200) {
            this.$message({
              type: 'success',
              message: '编辑成功!',
            })
            this.visible = false
            this.$emit('refreshDataList')
          } else {
            this.$message({
              type: 'error',
              message: res.message,
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'error',
            message: '系统异常',
          })
        })
    },
    init(data) {
      this.visible = true
      if (data.endTime) {
        data.endTimeStr = util.ilinkTimeformat.format(
          new Date(data.endTime),
          'yyyy-MM-dd'
        )
      }
      this.dataForm = Object.assign({}, data)
      this.pickerOptions.disabledDate = (time) => {
        return (
          time.getTime() <= data.startTime ||
          time.getTime() < new Date().setHours(0, 0, 0, 0)
        )
      }
    },
  },
}
</script>
<style lang='scss'>
</style>